import { ImportExcelDto } from '../dto/import-excel.dto';

/**
 * Excel数据验证工具类
 */
export class ExcelValidatorUtil {
  /**
   * 验证Excel行数据的完整性
   * @param data 导入的数据
   * @param rowIndex 行索引（用于错误提示）
   * @returns 验证结果
   */
  static validateRowData(data: ImportExcelDto, rowIndex: number): ValidationResult {
    const errors: string[] = [];
    const warnings: string[] = [];

    // 必填字段验证
    if (!data.matchTime) {
      errors.push(`第${rowIndex}行：开赛时间不能为空`);
    }

    if (!data.leagueName) {
      errors.push(`第${rowIndex}行：赛事名称不能为空`);
    }

    if (!data.homeTeam) {
      errors.push(`第${rowIndex}行：主队名称不能为空`);
    }

    if (!data.awayTeam) {
      errors.push(`第${rowIndex}行：客队名称不能为空`);
    }

    // 赔率数据验证
    const hasAnyOdds = data.winOdds || data.drawOdds || data.loseOdds;
    if (!hasAnyOdds) {
      warnings.push(`第${rowIndex}行：没有任何赔率数据`);
    }

    // 赔率值合理性验证
    if (data.winOdds && (data.winOdds <= 0 || data.winOdds > 100)) {
      errors.push(`第${rowIndex}行：胜赔率值不合理 (${data.winOdds})`);
    }

    if (data.drawOdds && (data.drawOdds <= 0 || data.drawOdds > 100)) {
      errors.push(`第${rowIndex}行：平赔率值不合理 (${data.drawOdds})`);
    }

    if (data.loseOdds && (data.loseOdds <= 0 || data.loseOdds > 100)) {
      errors.push(`第${rowIndex}行：负赔率值不合理 (${data.loseOdds})`);
    }

    // 让球数据验证
    if (data.jingcaiHandicap && Math.abs(data.jingcaiHandicap) > 10) {
      warnings.push(`第${rowIndex}行：竞彩让球数值较大 (${data.jingcaiHandicap})`);
    }

    if (data.beidanHandicap && Math.abs(data.beidanHandicap) > 10) {
      warnings.push(`第${rowIndex}行：北单让球数值较大 (${data.beidanHandicap})`);
    }

    // 编号格式验证
    if (data.jingcaiNumber && !this.isValidJingcaiNumber(data.jingcaiNumber)) {
      warnings.push(`第${rowIndex}行：竞彩编号格式可能不正确 (${data.jingcaiNumber})`);
    }

    if (data.beidanNumber && !this.isValidBeidanNumber(data.beidanNumber)) {
      warnings.push(`第${rowIndex}行：北单编号格式可能不正确 (${data.beidanNumber})`);
    }

    return {
      isValid: errors.length === 0,
      errors,
      warnings,
    };
  }

  /**
   * 验证竞彩编号格式
   * @param number 竞彩编号
   * @returns 是否有效
   */
  private static isValidJingcaiNumber(number: string): boolean {
    // 竞彩编号通常是数字格式，长度在3-10位之间
    const pattern = /^\d{3,10}$/;
    return pattern.test(number);
  }

  /**
   * 验证北单编号格式
   * @param number 北单编号
   * @returns 是否有效
   */
  private static isValidBeidanNumber(number: string): boolean {
    // 北单编号通常是数字格式，长度在3-10位之间
    const pattern = /^\d{3,10}$/;
    return pattern.test(number);
  }

  /**
   * 批量验证Excel数据
   * @param dataList 数据列表
   * @returns 批量验证结果
   */
  static validateBatchData(dataList: ImportExcelDto[]): BatchValidationResult {
    const results: ValidationResult[] = [];
    const allErrors: string[] = [];
    const allWarnings: string[] = [];

    dataList.forEach((data, index) => {
      const result = this.validateRowData(data, index + 2); // Excel行号从2开始（第1行是标题）
      results.push(result);
      allErrors.push(...result.errors);
      allWarnings.push(...result.warnings);
    });

    const validCount = results.filter(r => r.isValid).length;
    const invalidCount = results.length - validCount;

    return {
      totalCount: dataList.length,
      validCount,
      invalidCount,
      allErrors,
      allWarnings,
      results,
    };
  }
}

/**
 * 验证结果接口
 */
export interface ValidationResult {
  isValid: boolean;
  errors: string[];
  warnings: string[];
}

/**
 * 批量验证结果接口
 */
export interface BatchValidationResult {
  totalCount: number;
  validCount: number;
  invalidCount: number;
  allErrors: string[];
  allWarnings: string[];
  results: ValidationResult[];
}